package ;

import flixel.FlxG;
import flixel.FlxSprite;
import flixel.group.FlxTypedGroup;
import flixel.text.FlxText;
import flixel.util.FlxColor;
import flixel.util.FlxColorUtil;
import haxe.Resource;
import openfl.Assets;

/**
 * ...
 * @author ...
 */
class Conversation extends FlxTypedGroup<FlxSprite>
{
	/* Okay so what does a conversation need
	 * A speech bubble (flxsprite)
	 * A place to put all the characters (here?)
	 * The text (xml)
	 * A place to display the text (flxtext)
	 */
	
	 /* Ideas for future addtions:
	  * Choices
	  * Different emotions (bigger spritesheets)
	  */
	
	 
	private var _speech:FlxText;
	private var _speechBubble:FlxSprite;
	private var _character:FlxSprite;
	private var _txtDoc:Xml;
	private var _fullConvo:Iterator<Xml>;
	private var _currentChar:Iterator<Xml>;
	private var _onRight:Bool;
	
	private var _screenFade:FlxSprite;
	 
	public function new(convoSource:String) 
	{
		super();
		
		// Create the faded screen overlay
		_screenFade = new FlxSprite().makeGraphic(FlxG.width, FlxG.height + 80, FlxColorUtil.makeFromARGB(180, 0, 0, 0));
		_screenFade.scrollFactor.set();
		add(_screenFade);
		
		// Create the speech bubble
		_speechBubble = new FlxSprite(80, FlxG.height - 210, AssetPaths.speechbubble__png);
		_speechBubble.scrollFactor.set();
		add(_speechBubble);
		// Load in the conversation xml
		_txtDoc = Xml.parse(Assets.getText(convoSource));
		_fullConvo = _txtDoc.firstElement().elements();
		_currentChar = Xml.createElement("blank").elements();
		// Set up the text area
		_speech = new FlxText(120, FlxG.height - 203, 420, "", 18);
		_speech.color = FlxColor.BLACK;
		_speech.scrollFactor.set();
		add(_speech);
		// Set up the character sprite
		_character = new FlxSprite();
		_character.scrollFactor.set();
		add(_character);
		_onRight = true;
		// Load in the first character and line
		nextLine();
	}
	
	override public function update():Void 
	{
		#if !FLX_NO_MOUSE
		if (FlxG.mouse.justPressed)
		{
			nextLine();
		}
		#end
		#if !FLX_NO_TOUCH
		var didSelect:Bool = false;
		if (FlxG.touches.justReleased().length > 0)
		{
			nextLine();
		}
		#end
		super.update();
	}
	
	private function nextLine()
	{
		if (_currentChar.hasNext())
		{
			var newLine:Xml = _currentChar.next();
			_speech.text = newLine.firstChild().nodeValue;
			/*if (newLine.get("mood") != null)
			{
				_character.animation.play(newLine.get("mood"));
			}
			if (newLine.nodeName == "choice") {
				// do some stuff here if choices get implemented
				// iterate over children I guess
			}*/
		}
		else if (_fullConvo.hasNext())
		{
			var newChar:Xml = _fullConvo.next();
			_currentChar = newChar.elements();
			var newLine:Xml = _currentChar.next();
			_speech.text = newLine.firstChild().nodeValue;
			_character.loadGraphic("assets/images/" + newChar.nodeName + ".png", true,161,164);
			_character.setPosition(0, FlxG.height - _character.height - 110);
			//_character.animation.add("default", [0, 1], 4);
			//_character.animation.add("happy", [2, 3], 4);
			//if (newLine.get("mood") != null)
			//{
			//	_character.animation.play(newLine.get("mood"));
			//}
			//else
			//{
			//	_character.animation.play("default");
			//}
			if (_onRight)
			{
				_character.x = 0;
				_character.flipX = false;
				_speechBubble.flipX = false;
				_speechBubble.x = 80;
				_speech.x = 140;
				_onRight = false;
			}
			else
			{
				_character.x = FlxG.width - _character.width - 0;
				_character.flipX = true;
				_speechBubble.flipX = true;
				_speechBubble.x = 17;
				_speech.x = 25;
				_onRight = true;
			}
		}
		else {
			active = false;
			visible = false;
		}
	}
	
}